export default class Comic {
  constructor(data = {}) {
    this.comicId = data.comic_id;
    this.comicName = data.comic_name;
    this.latestChapter = new LatestChapter(data.latest);
    this.createTime = data.update_time;
    this.hasCopyright = Boolean(data.copyright);
    this.chapterMap = new ChapterMap();
    this.cover = data.cover;
    this.cdn = data.cdn;

    const { curr = {} } = data;
    this.setCurrentChapterId(curr.chapter_id);
    this.setChapterMap(data);
  }

  setChapterMap(data) {
    const { curr = {}, prev, next } = data;
    const chapterList = [prev, curr, next];
    chapterList.forEach((chapter, index) => {
      if (!!chapter) {
        const prevChapter = chapterList[index - 1] || {};
        const nextChapter = chapterList[index + 1] || {};
        chapter.prevChapterId = prevChapter.chapter_id;
        chapter.nextChapterId = nextChapter.chapter_id;
        const chapterInstance = new Chapter(chapter, this.comicName);
        const { prevChapterId, chapterId, nextChapterId } = chapterInstance;
        let prevChapterInstance = this.chapterMap[prevChapterId];
        let nextChapterInstance = this.chapterMap[nextChapterId];
        if (prevChapterInstance) {
          prevChapterInstance.nextChapterId = chapterId;
        }
        if (nextChapterInstance) {
          nextChapterInstance.prevChapterId = chapterId;
        }
        let currentChapterInstance = this.chapterMap[chapterId];
        if (currentChapterInstance) {
          if (nextChapterId) {
            currentChapterInstance.nextChapterId = nextChapterId;
          }
          if (prevChapterId) {
            currentChapterInstance.prevChapterId = prevChapterId;
          }
        } else {
          this.chapterMap[chapterId] = chapterInstance;
        }
      }
    })
  }
  setCurrentChapterId(currentChapterId) {
    this.currentChapterId = currentChapterId;
  }

  coverCDN(type) {
    return this.cdn + this.cover[type];
  }
}

export class ChapterMap {
  set(chaperId, chapterInfo) {
    if (!this[chaperId]) {
      this[chaperId] = chapterInfo;
    }
  }
}

export class ChapterSEO {
  constructor(comicName, chapterName, data = {}) {
    this.title = data.title || `${comicName} ${chapterName}`,
      this.keywords = data.keywords || this.getKeywords(comicName, chapterName),
      this.description = data.description || '-'
  };
  getKeywords(comicName, chapterName) {
    const chapterList = chapterName.split(' ').filter(item => !!item.trim());
    return `${comicName}漫画,${comicName}${chapterList[0]},${chapterList.join(',')}`
  }
}

export class ChapterImage {
  constructor(data) {
    this.chapterId = data.chapterId || -1 // 章节id
    this.chapterIndex = data.chapterIndex + 1; // 图片在本章节中的序号
    this.src = data.src || ''; // 图片src地址

    // 图片加载相关参数
    this.width = 0; // 图片宽度
    this.height = 0; // 图片高度
    this.img = null; // 图片加载Node节点
    this.count = 0; // 图片加载次数
    this.status = 'init'; // 图片加载状态
    this.target = null;
    this.isPushEvent = false; // 是否发送事件统计
    this.isRetry = false; // 重试
  }

}
export class LatestChapter {
  constructor(data = {}) {
    this.chapterId = parseInt(data.chapter_id, 10) || 0;
    this.chapterNumName = parseInt(data.chapter, 10) ? `第${data.chapter}话` : data.chapter; // this.chapterName.split(' ')[0];
    this.chapterName = `${this.chapterNumName} ${data.chapter_name}` || '-';
    this.createTime = data.create_time || '-';
  }
}

export class Chapter extends LatestChapter {
  constructor(data = {}, comicName) {
    super(data);
    this.price = data.price || 0;
    this.chapterList = this.getChapterList(data.images);
    this.chapterSEO = new ChapterSEO(comicName, this.chapterName, data);
    this.domain = data.domain;
    this.chapterCover = data.chapter_cover;
    this.setAdjacentChapterId(data);
  }
  getChapterList(data = []) {
    const tmpArr = []
    data.forEach((src, chapterIndex) => {
      // 这里是测试，将图片地址搞错
      // const s = chapterIndex % 2;
      // src = s ? src + '@@' : src;
      tmpArr.push(new ChapterImage({ src, chapterIndex, chapterId: this.chapterId }));
    });
    return tmpArr;
  }
  setAdjacentChapterId(data = {}) {
    this.prevChapterId = data.prevChapterId;
    this.nextChapterId = data.nextChapterId;
  }
}
